package com.tjetc.view.studentview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.LeaveMsg;
import com.zhiyun.service.LeaveMsgService;
import com.zhiyun.vo.AppData;
import com.zhiyun.vo.Result;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class MyCallAdmin {
    LeaveMsgService leaveMsgService= CilentProxy.getProxyInstance(LeaveMsgService.class);
    Scanner scanner=new Scanner(System.in);
    public void start(){
        while (true){
            System.out.println("~~~~~~~~~联系管理员~~~~~~~~");
            System.out.println("1.去联系");
            System.out.println("2.查看我的留言");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    call();
                    break;
                case 2:
                    look();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误,请重新输入");
            }
        }
    }

    private void call() {
        while (true){
            System.out.println("请输入留言");
            String liuyan = scanner.next();
            Result result = leaveMsgService.addLeaveMsg(new LeaveMsg(null, AppData.student.getUserId(), null, liuyan, null, new Date(), null));
            if (result.getCode()==200){
                System.out.println("留言成功");
            }else {
                System.out.println("留言失败");
            }
            break;
        }
    }

    private void look() {
        while (true){
            Result result = leaveMsgService.MyLeaveMsg(AppData.student.getUserId());
            if (result.getCode()==200){
                List<LeaveMsg> obj = (List<LeaveMsg>) result.getObj();
                for (LeaveMsg leaveMsg : obj) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    // 将Date转换为String
                    String dateString = sdf.format(leaveMsg.getCreateTime());
                    System.out.println(leaveMsg.getId()+"    内容:"+leaveMsg.getMsg()+"       发布时间"+dateString);
                    if (leaveMsg.getAdminId()!=null){
                        String format = sdf.format(leaveMsg.getResolveTime());
                        System.out.println("回复管理员id"+leaveMsg.getAdminId()+"   内容"+leaveMsg.getResponseMsg()+"    回复时间"+format);
                    }
                }
            }else {
                System.out.println("我的留言加载失败");
            }
            break;
        }
    }
}
